/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012 Oliver Katz */

#ifndef __VISUAL_BACKEND_DIRECTFB_H
#define __VISUAL_BACKEND_DIRECTFB_H

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <sstream>

#include <directfb.h>

#define DFB_CHECK(x...) {DFBResult e = x; if (e != DFB_OK) {cout << "- directfb error\n\t"; DirectFBErrorFatal(#x, e);}}

using namespace std;

namespace sleepyface
{
	namespace visual
	{
		//DFBEnumerationResult enumerateInputDevice(unsigned int id, DFBInputDeviceDescription dsc, void *data);

		class Backend
		{
		private:
			IDirectFB *dfb;
			IDirectFBSurface *pri;
			IDirectFBEventBuffer *ebuf;

			int width;
			int height;
			string name;

			map<string, IDirectFBFont *> fonts;

			map<unsigned int, DFBInputDeviceDescription> devs;

			void (*displayCallback)(void);
			void (*keyPressCallback)(unsigned char k, int x, int y);
			void (*keyReleaseCallback)(unsigned char k, int x, int y);
			void (*hoverCallback)(int x, int y);
			void (*dragCallback)(int x, int y);
			void (*buttonCallback)(int b, int s, int x, int y);

			bool run;

		public:
			Backend();
			int init(int *argc, char ***argv);
			int async();
			int halt();
			string setName(string n);
			string getName();
			int getWidth();
			int setWidth(int w);
			int getHeight();
			int setHeight(int h);
			int rect(int x0, int y0, int x1, int y1, int r, int g, int b, int a);
			int line(int x0, int y0, int x1, int y1, int r, int g, int b, int a);
			int imageGetSize(string path, int &w, int &h);
			int image(int x, int y, string path);
			int loadFont(string name, string path, int size);
			int unloadFont(string name);
			int getTextWidth(string font, string text);
			int text(int x0, int y0, int r, int g, int b, int a, string font, string s);
			int update();
			int bindDisplay(void (*cb)(void));
			int bindKeyPress(void (*cb)(unsigned char k, int x, int y));
			int bindKeyRelease(void (*cb)(unsigned char k, int x, int y));
			int bindHover(void (*cb)(int x, int y));
			int bindDrag(void (*cb)(int x, int y));
			int bindButton(void (*cb)(int b, int s, int x, int y));
			IDirectFB *getDFB();
			IDirectFBSurface *getDFBSurface();
		};

		extern "C"
		{
			extern Backend *_vb;
		}
		
		#define Z_GET_VISUAL_BACKEND() (*_vb)
		#define Z_SET_VISUAL_BACKEND(b) (*_vb = b)
	}
}

#endif